package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.config.utils.MD5;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;



/**
 * @author wans
 * @create 2021-11-05 19:44
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;
    
    @GetMapping("findAll")
    public Result findallHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean remove = hospitalSetService.removeById(id);
        if(remove){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    //查询医院信息带分页结果
    @PostMapping("{current}/{limit}")
    public Result findPageHospSet(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //有可能用户端输入的查询条件值为空
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        return Result.ok(pageHospitalSet);
    }
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet (@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(
                MD5.encrypt(
                        System.currentTimeMillis()+""
                                +random.nextInt(1000))
        );
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return  Result.ok();
        }else{
            return Result.fail();
        }
    }

    @GetMapping("getHospSet/{id}")
    public Result getHospSet (@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }else{
            return Result.fail();
        }

    }

    //以id批量删除
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //锁定或解锁医院数据
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        System.out.println("-------------------------" +
                "lockHospitalSet");
        return Result.ok();
    }
    //发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result getSignKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String key = hospitalSet.getSignKey();
        //TODO 发送短信
        return Result.ok();
    }
    
    

}
    

